package com.lft01.thread;

import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

/**
 * Function: 		组合式异步编程
 * Reason:   		ADD REASON.
 * Date             2020/6/6 9:09
 * <p>
 * Class Name:      CallableTest1
 * Package Name:    com.lft.thread
 *
 * @author Laifengting
 * @version 1.0.0
 * @since JDK 8
 */
public class CallableTest1 {
	public static void main(String[] args) {
		System.out.println("开始核算工程总成本");
		//工程成本核算问题
		//一条线程计算人工成本
		FutureTask<Double> doubleFutureTask1 = new FutureTask<>(() -> {
			Thread.sleep(3000);
			System.out.println("开始核算人工成本");
			return 12.0d;
		});
		
		new Thread(doubleFutureTask1).start();
		
		//一条线程计算材料成本
		FutureTask<Double> doubleFutureTask2 = new FutureTask<>(new Callable<Double>() {
			@Override
			public Double call() throws Exception {
				Thread.sleep(6000);
				System.out.println("开始核算材料成本");
				return 15.6d;
			}
		});
		new Thread(doubleFutureTask2).start();
		
		//再启动一条线程核算
		FutureTask<Double> doubleFutureTask3 = new FutureTask<>(new Callable<Double>() {
			@Override
			public Double call() throws Exception {
				Thread.sleep(9000);
				System.out.println("开始再次核算材料成本");
				return 15.6d;
			}
		});
		
		new Thread(doubleFutureTask3).start();
		//主线各计算总成本
		try {
			Double laborCost = doubleFutureTask1.get();
			System.out.println("人工成本为：" + laborCost);
			
			Double materialCost = doubleFutureTask2.get();
			System.out.println("材料成本为：" + materialCost);
			
			Double materialCost2 = doubleFutureTask3.get();
			System.out.println("核算材料成本为：" + materialCost2);
			
			if (materialCost.equals(materialCost2)) {
				Double totalCost = laborCost + materialCost;
				System.out.println("总成本为：" + totalCost);
			} else {
				System.out.println("材料成本核算不准备，请重新核算");
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
}
